/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
///////////change log (new above)/////////
// added bad read counters, to stop sporadic misreadings to reset the bus
//////////////////////////////////////////
#ifndef FRMPOWER_H
#define FRMPOWER_H

#include <QWidget>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QHostAddress>
#include <QTimer>
#include "ui_frmpower.h"
#include "gssglobals.h"
#include "hpsceventthread.h"
#include "qhkp.h"



/**
The Power controls widget

	@author Archimedes Projekt <thelsb@gmail.com>
*/
class FrmPower : public QWidget
{
Q_OBJECT
 public:
     typedef enum { NONE=0,A,B,C,D } BUS;
     typedef enum { POWER_OFF, POWER_OK, UNDERVOLT, OVERVOLT, AMPLIMIT} POWER_STAT;
     typedef enum { FLIGHT,FLIGHT_TEST,CHECKOUT,CHARGE } MODE;
    
     
     
private:
  static FrmPower::BUS m_BorC;
  int m_badReadCntA_V,
      m_badReadCntA_A,
      m_badReadCntB_V,
      m_badReadCntB_A,
      m_badReadCntC_V,
      m_badReadCntC_A,
      m_badReadCntD_V,
      m_badReadCntD_A,
      m_badReadLimit;
  HPSCEventThread *m_hpscEventThread;
  BUS m_bus1,
      m_bus2;
  QTimer m_timerAlive,
         m_timerHkpPoll;
  QHostAddress m_hkpHost,
               m_hpscHost;
  quint16 m_hkpPort,
          m_hpscPort;
  QTcpSocket *m_hkpSocket;

  Ui::FrmPower ui;
  
  float m_busAmaxA,
        m_busAminA,
        m_busBmaxA,
        m_busBminA,
        m_busCmaxA,
        m_busCminA,
        m_busDmaxA,
        m_busDminA,
        m_busAmaxV,
        m_busAminV,
        m_busBmaxV,
        m_busBminV,
        m_busCmaxV,
        m_busCminV,
        m_busDmaxV,
        m_busDminV,
        m_busAtoleranceV,
        m_busAtoleranceA,
        m_busBtoleranceV,
        m_busBtoleranceA,
        m_busCtoleranceV,
        m_busCtoleranceA,
        m_busDtoleranceV,
        m_busDtoleranceA;
  
  float m_src1v,      //soll values
        m_src1a,
        m_src2v,
        m_src2a,
        m_src3v,
        m_src3a,
        m_src4v,
        m_src4a,
        m_src1vRead,  //read values
        m_src2vRead,
        m_src3vRead,
        m_src4vRead,
        m_src1aRead,
        m_src2aRead,
        m_src3aRead,
        m_src4aRead;
  
protected:
        FrmPower::POWER_STAT checkBusV(BUS bus);
    
        FrmPower::POWER_STAT checkBusA(BUS bus);
        
        FrmPower::POWER_STAT checkBus(FrmPower::BUS bus);
                    
public:
    
    FrmPower(BUS bus1,BUS bus2,QWidget *parent = 0);

    ~FrmPower();
    
    void setHPSCEventThread(HPSCEventThread *hpscThread);
    
    static FrmPower::BUS getBorC();
    
    static void setBorC(FrmPower::BUS bc);
    
    void setAtolerance(BUS bus, float tolerance);
    
    void setVtolerance(BUS bus, float tolerance);
    
    void setStatusBus2(BUS bus,POWER_STAT status);
    
    void setStatusBus1(BUS bus,POWER_STAT status);
    
    
	signals:
        
		void sig_hpscCommand(HPSC_Command,int);
        void sig_busStatus(FrmPower::BUS,FrmPower::POWER_STAT);
        
public slots:
    
    PStyledButton * btnBus1();
    
    PStyledButton * btnBus2();
    
    void setAlabel(int busNr,float val);
    
    void setVlabel(int busNr,float val);
    
    void bus2(bool checked);
    
    void bus1(bool checked);
    
    void connectHandler();
    
    void hpscDisconnected();
    
    void setBus2();
    
    void setBus1();
    
    void hpscReadoutV(int src,float v);
    
    void hpscReadoutA(int src,float a);
    
    void getHkpStatus(HkpCommand hkpPacket);
};



#endif
